package com.matriq.cloudmanager;

import com.matriq.cloudmanager.R;
import com.matriq.cloudmanager.jo.KeyValueJo;
import com.matriq.cloudmanager.service.AccountService;

import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import net.lgpage.activity.BaseActivity;
import net.lgpage.http.ExceptionAsyncTask;
import net.lgpage.util.Checker.StringChecker;
import net.lgpage.util.Cleaner;

public class EditValueActivity extends BaseActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_edit_value);
		setViews();
		setEvents();
		loadData();
	}

	TextView tv_title;
	EditText et_content;
	ImageButton ll_back;
	Button bt_save;

	private void setViews() {
		tv_title = (TextView) this.findViewById(R.id.tv_title);
		et_content = (EditText) this.findViewById(R.id.et_content);
		ll_back = (ImageButton) this.findViewById(R.id.ll_back);
		bt_save = (Button) this.findViewById(R.id.bt_save);
	}

	private void setEvents() {
		bt_save.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (checkEts()) {
					 putObj();
				}
			}
		});
	}

	Object copyObj;

	public boolean checkEts() {
		if (copyObj == null) {
			copyObj = new Object();
		}

		String str_et_content = et_content.getEditableText().toString();

		if (StringChecker.isNull(str_et_content)) {
			setError(title + "不能为空", et_content);
			return false;
		}
		if (StringChecker.containsSpecial(str_et_content)) {
			if(!title.contains("单位及职务")){
				setError(title + "不能包含特殊字符", et_content);
				return false;
			}
		}
		if (title.contains("手机") && !StringChecker.is1Tel(str_et_content)) {
			setError(title + "手机格式不正确", et_content);
			return false;
		}
		if (title.contains("电话") && !StringChecker.isPhoneNum(str_et_content)) {
			setError(title + "电话格式不正确", et_content);
			return false;
		}
		
		return true;
	}

	ObjTask objTask;

	public void putObj() {
		if (objTask != null) {
			objTask.cancel(true);
		}
		objTask = new ObjTask(this);
		objTask.execute();
	}

	private class ObjTask extends ExceptionAsyncTask<Object> {

		public ObjTask(Context context) {
			super(context);
		}

		@Override
		public Object doInBackgroundX(String... params) throws Exception {
			KeyValueJo jo = new KeyValueJo();
			jo.setKey(key);
			jo.setValue(et_content.getEditableText().toString());
			return AccountService.getService().info_mode(jo);
		}

		@Override
		public void onPostExecuteX(Object result) {
			if (result == null) {
				return;
			} else {
				setResult(200);
				getActivity().finish();
				// Snackbar.make(this, "成功", 1).show();
			}
		}
	}

	public void setData() {
		tv_title.setText(title);
		et_content.setText(value);
		if (title.contains("手机") || title.contains("电话") || title.contains("邮政编码") || title.contains("身份证")) {
			et_content.setInputType(InputType.TYPE_CLASS_PHONE);
		} else {
			et_content.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
		}
		et_content.setSelection(et_content.length());
	}

	String title;
	String key;
	String value;

	private void loadData() {
		title = getIntent().getStringExtra("title");
		key = getIntent().getStringExtra("key");
		value = getIntent().getStringExtra("value");
		setData();
	}

	@Override
	public void onDestroy() {
		Cleaner.cleanTask(objTask);
		super.onDestroy();
	}

}